<template>
  <div class="ring-chart-row">
    <div class="ring-table">
      <table style="width:320px" v-if="type=='pe'">
        <thead>
          <tr>
            <th width="80px">钢种</th>
            <th width="80px">实际酸洗速度</th>
            <th width="80px">理论酸洗速度</th>
            <th>总产量</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>30</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>30</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>30</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>30</td>
          </tr>
        </tbody>

      </table>
      <table style="width:320px" v-else-if="type=='ap'">
        <thead>
          <tr>
            <th width="80px">钢种</th>
            <th width="80px">实际机时产量</th>
            <th width="80px">理论机时产量</th>
            <th>停机次数</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
        </tbody>
        
      </table>
      <table style="width:320px" v-else-if="type=='tpe'">
        <thead>
          <tr>
            <th width="80px">班组</th>
            <th width="80px">产量</th>
            <th width="80px">机时产量</th>
            <th>生产效率</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
        </tbody>
        
      </table>
      <table style="width:320px" v-else-if="type=='ur'">
        <thead>
          <tr>
            <th width="80px">原料厚度</th>
            <th width="80px">活套使用率</th>
            <th width="80px">机时产量</th>
            <th>总产量</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
          <tr>
            <td>#205</td>
            <td>30</td>
            <td>30</td>
            <td>3</td>
          </tr>
        </tbody>
        
      </table>
    </div>
    <div class="ring-chart">
      <div ref="chartRef" class="chart"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, nextTick } from 'vue'
import * as echarts from 'echarts/core'
import { PieChart } from 'echarts/charts'
import { TitleComponent, TooltipComponent, LegendComponent } from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'

echarts.use([
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  CanvasRenderer
])

const props = defineProps<{
  data: { value: number, name: string, color?: string }[],
  title?: string,
  type?:string
}>()

const chartRef = ref<HTMLDivElement | null>(null)
let chartInstance: echarts.ECharts | null = null

const renderChart = () => {
  if (!chartRef.value) return
  if (!chartInstance) {
    chartInstance = echarts.init(chartRef.value, undefined, { renderer: 'canvas' })
  }
  const option = {
    backgroundColor: 'transparent',
    title: {
      text: '',
      left: 'center',
      top: 10,
      textStyle: {
        color: '#fff',
        fontSize: 12
      }
    },
    maintainAspectRatio: false, // 允许自定义宽高
    width: 110,  // 设置宽度
    height:110, // 设置高度
    tooltip: {
      trigger: 'item',
      backgroundColor: 'rgba(0,0,0,0.7)',
      borderColor: '#2196f3',
      textStyle: { color: '#fff' }
    },
    legend: {
      orient: 'vertical',
      left: 40,
      top: 140,
      textStyle: { color: '#b0c9e0' }
    },
    series: [
      {
        name: props.title || '',
        type: 'pie',
        radius: ['60%', '80%'],
        center: ['65%', '85%'],
        avoidLabelOverlap: false,
        label: {
          show: true,
          position: 'center',
          formatter: () => {
            const total = props.data.reduce((sum, d) => sum + d.value, 0)
            return total
          },
          fontSize: 12,
          color: '#00eaff',
          fontWeight: 'bold'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 12,
            fontWeight: 'bold',
            color: '#fff'
          }
        },
        labelLine: {
          show: false
        },
        data: props.data.map(d => ({
          value: d.value,
          name: d.name,
          itemStyle: d.color ? { color: d.color } : undefined
        }))
      }
    ]
  }
  chartInstance.setOption(option)
}

onMounted(() => {
  nextTick(renderChart)
})

watch(() => props.data, renderChart, { deep: true })
</script>

<style scoped>
.ring-chart-row {
  display: flex;
  flex-direction: row;
  width: 100%;
  height: 220px;
  background: #14233c;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.ring-table {
  flex: 1 1 0;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  padding: 0;
  margin: 0;
  height: 100%;
}
.ring-table table {
  width: 100%;
  height: 100%;
  border-collapse: collapse;
  background: #1a2b47;
  border-radius: 6px 0 0 6px;
  overflow: hidden;
  font-size: 12px;
  margin: 0;
  padding: 0;
}
.ring-table th, .ring-table td {
  border: 1px solid #23344a;
  color: #fff;
  text-align: center;
  padding: 4px 0;
  margin: 0;
}
.ring-table th {
  background: #184a8c;
  font-weight: bold;
  font-size: 12px;
  border-bottom: 1.5px solid #2196f3;
}
.ring-chart {
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.chart {
  width: 100%;
  height: 100%;
}
</style>
